//
//  login_loginViewController.m
//  ziLu_ios
//
//  Created by mac on 16/3/18.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "login_loginViewController.h"

@interface login_loginViewController ()

@end

@implementation login_loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor colorWithRed:239.0/255.0 green:239.0/255.0 blue:239.0/255.0 alpha:1];
    
    
    // Do any additional setup after loading the view from its nib.
}
- (IBAction)logIn:(id)sender {
   
    if ([self checkTel:_shouJi.text]) {
        
        NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys:_shouJi.text,@"mobile",_miMa.text,@"password",@"1",@"channel",@"1",@"system",nil];
      [afn_api MyApi:dictionary url:AFN_Address(@"/user/login") success:^(id responseObject) {
          
          NSLog(@"%@",responseObject);
          if ([[responseObject objectForKey:@"status"] isEqualToString:@"ok"]) {
              //数据持久化
            NSString *token = responseObject[@"user"][@"userId"];
              [[NSUserDefaults standardUserDefaults] setObject:token forKey:@"userId"];
              [[NSUserDefaults standardUserDefaults] setObject:_shouJi.text forKey:@"mobile"];
              [[NSUserDefaults standardUserDefaults] synchronize];
              NSLog(@"token = %@",token);
              [self.navigationController popViewControllerAnimated:TRUE];
              
          }
      } failure:^(NSError *error) {
           NSLog(@"%@",error);
      }];
        
        
    }
    
}
#pragma mark - 验证手机号码
- (BOOL)checkTel:(NSString *)str

{
    
    if ([str length] == 0) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"手机号为空" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        
        [alert show];
        
        return NO;
        
    }
    
    //1[0-9]{10}
    
    //^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$
    
    //    NSString *regex = @"[0-9]{11}";
    
    //    NSString *regex = @"^((13[0-9])|(147)|(15[^4,\\D])|(18[0,5-9]))\\d{8}$";
    NSString * regex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    BOOL isMatch = [pred evaluateWithObject:str];
    
    if (!isMatch) {
        
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入正确的手机号码" delegate:nil cancelButtonTitle:@"好的" otherButtonTitles:nil];
        
        [alert show];
        
        
        return NO;
        
    }
    
    
    return YES;
    
}
#pragma mark - 收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self.shouJi resignFirstResponder];
    [self.miMa resignFirstResponder];
  
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
